package descarte.com.br.descarte;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.SQLException;

import descarte.com.br.descarte.Bean.Fornecedor;
import descarte.com.br.descarte.Bo.FornecedorBo;

/**
 * Created by jvba on 22/03/2015.
 */
public class LoginActivity extends Activity {

    Button cadastrar;
    Button entrar;
    EditText login;
    EditText senha;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        bind();
    }

    private void bind() {

        cadastrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent in = new Intent(LoginActivity.this, CadastroActivity.class);
                startActivity(in);
            }
        });

        entrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long doc = Long.parseLong(login.getText().toString());

                try {
                    FornecedorBo cbo = new FornecedorBo(LoginActivity.this);
                    Fornecedor fornecedor = cbo.queryById(doc);

                    if (fornecedor.getSenha().equals(senha.getText().toString())) {
                        Intent in = new Intent(LoginActivity.this, MainActivity.class);

                        startActivity(in);
                    } else {
                        Toast.makeText(LoginActivity.this, "Login ou Senha validas", Toast.LENGTH_LONG).show();
                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    private void init() {
        cadastrar = (Button) findViewById(R.id.cadastro);
        entrar = (Button) findViewById(R.id.entrar);
        login = (EditText) findViewById(R.id.login);
        senha = (EditText) findViewById(R.id.senha);
    }

}
